package edu.cmu.mism.dgjava.data.ui.composites.option;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;

import edu.cmu.mism.dgjava.data.models.option.BaseType;
import edu.cmu.mism.dgjava.data.ui.composites.AbstractDataComposite;
import edu.cmu.mism.dgjava.utils.SWTUtils;
import edu.cmu.mism.dgjava.views.Messages;

/**
 * 
 * @author Christian
 * 
 */
public abstract class OptionInputDataComposite extends AbstractDataComposite {

	protected Button _putRadio;
	protected Button _callRadio;

	@Override
	public void createControls(Composite parent) {
		Composite container = new FormToolkit(parent.getDisplay())
				.createComposite(parent, SWT.BORDER);
		GridLayout gl = new GridLayout(3, false);

		container.setLayout(gl);
		
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		// create custom controls
		createCustomControls(container);

		// spacer
		SWTUtils.createSpacer(container, 1);

		// call/put radio button group
		createBaseTypeGroup(container);
		
		hookupModifyListener();

		validate();

	}


	private void createBaseTypeGroup(Composite container) {
		Composite radioComp = SWTUtils.createComposite(container, 2, -1, 1);
		this._putRadio = SWTUtils.createRadioButton(radioComp,
				Messages.OptionDataView_radio_put);
		this._callRadio = SWTUtils.createRadioButton(radioComp,
				Messages.OptionDataView_radio_call);
		this._putRadio.setSelection(true);
	}

	/**
	 * Return the user specified option base type
	 * 
	 * @see BaseType
	 * @return <tt>BaseType.PUT</tt> or <tt>BaseType.CALL</tt>
	 */
	protected BaseType getOptionBaseType() {
		if (this._putRadio != null && this._callRadio != null) {
			if (this._putRadio.getSelection()) {
				return BaseType.PUT;
			}

			if (this._callRadio.getSelection()) {
				return BaseType.CALL;
			}
		}

		return null;
	}

	/**
	 * This method should be implemented by each option type composite children
	 * to offer customized controls for each option type
	 * 
	 * @param parent
	 */
	protected abstract void createCustomControls(Composite parent);

}
